package com.youmingxing.activity;

import android.os.Bundle;

import com.youmingxing.R;
import com.youmingxing.adpter.selfInfo.AuditionRankAdapter;
import com.youmingxing.bean.selfinfo.AuditionRank;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chihiro(xuchang)
 * @since 2015-10-11
 */

public class RankListActivity extends BaseActivity implements RefreshListView.OnLoadMoreListener {
    RefreshListView rankList;
    AuditionRankAdapter adapter;
    List<AuditionRank> auditionRankList = new ArrayList<>();
    int movie_id;
    int category_id;
    int mark = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_list_view);
        setTopbarName(R.string.title_activity_rank_list);
        initView();
        Bundle bundle = getIntent().getExtras();
        movie_id = bundle.getInt("movie_id");
        category_id = bundle.getInt("category_id");
        AllStarApi.getAuditionRank(movie_id, category_id, mark, rankHandler);

    }

    private void initView() {
        rankList = (RefreshListView) findViewById(R.id.list_view);
        rankList.setOnLoadMoreListener(this);
        adapter = new AuditionRankAdapter(this);
        adapter.setAuditionRankList(auditionRankList);
        rankList.setAdapter(adapter);
    }

    BaseJsonHandler rankHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            rankList.endLoadMore();
            JSONArray data = response.optJSONArray("data");
            if (data.length() < 20) {
                rankList.noMoreData();
            }
            for (int i = 0; i < data.length(); i++) {
                AuditionRank rank = new AuditionRank(data.optJSONObject(i));
                auditionRankList.add(rank);
            }
            adapter.notifyDataSetChanged();
            mark++;
        }
    };

    @Override
    public void onLoadMore() {
        AllStarApi.getAuditionRank(movie_id, category_id, mark, rankHandler);
    }
}
